<template>
  <el-row
    type="flex"
    justify="space-around"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col>
      {{ treeNode.name }}
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>
          <span>
            {{ treeNode.manager }}
          </span>
        </el-col>
        <el-col>
          <el-dropdown
            trigger="click"
            @click.native="$event.stopPropagation()"
            @command="handleCommand"
          >
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                command="edit"
              >编辑部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                command="del"
              >删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    treeNode: {
      required: true,
      type: Object
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleCommand(command) {
      if (command === 'add') {
        // 添加
        this.$emit('dialogShow', this.treeNode)
      } else if (command === 'edit') {
        // 编辑
        this.$emit('dialogShow', this.treeNode, true)
      } else {
        // 删除
        this.$confirm('是否删除此部门?', '删除部门', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return delDepartment(this.treeNode.id)
          })
          .then(() => {
            this.$emit('delDepartment')
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
      }
    }
  }
}
</script>

<style>
.el-dropdown:hover {
  cursor: pointer;
}
.el-dropdown {
  user-select: none;
}
</style>
